package priv.zgh.forum.discuss.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import priv.zgh.forum.auth.entity.User;
import priv.zgh.forum.auth.service.UserService;
import priv.zgh.forum.discuss.entity.Comment;
import priv.zgh.forum.discuss.entity.CommentAndUserInfoVo;
import priv.zgh.forum.discuss.entity.Posts;
import priv.zgh.forum.discuss.service.CommentService;
import priv.zgh.forum.discuss.service.PostsService;
import priv.zgh.forum.utils.R;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZGH
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/discuss/comment")
@CrossOrigin
public class CommentController {
    CommentService commentService;
    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }
    UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "按照传入条件来进行分页查询信息.")
    @PostMapping(path = "pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) Comment query){
        Page<Comment> page = commentService.pageResourcesCondition(current,limit,query);
        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());
    }

    @ApiOperation(value = "根据帖子ID，按照传入条件来进行分页查询信息.")
    @PostMapping(path = "pageConditionByPostsId/{postsId}/{current}/{limit}")
    public R pageCondition(@PathVariable String postsId,@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) Comment query){
        Page<Comment> page = commentService.pageResourcesConditionByPostsId(postsId,current,limit,query);
        List<CommentAndUserInfoVo> list = new ArrayList<>();
        for(Comment item : page.getRecords()){
            // 属性传递
            CommentAndUserInfoVo temp = new CommentAndUserInfoVo();
            BeanUtils.copyProperties(item,temp);
            // 赋值新属性
            User user = userService.getById(item.getReleaseUserId());
            temp.setReleaseUserNickname(user.getNickname());
            temp.setReleaseUserUsername(user.getUsername());
            list.add(temp);
        }
        return R.ok().data("total",page.getTotal()).data("rows",list);
    }

    @ApiOperation(value = "根据权限ID来获数据.")
    @GetMapping(path = "/getInfo/{id}")
    public R getInfoById(@PathVariable String id){
        Comment comment = commentService.getById(id);
        return R.ok().data("comment",comment);
    }

    @ApiOperation(value = "按照传入ID删除.")
    @DeleteMapping(path = "/delete/{id}")
    public R remote(@ApiParam(name = "id",value = "ID",required = true) @PathVariable(name = "id") String id){
        boolean flush = commentService.removeById(id);
        if(flush){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "保存")
    @PostMapping(path = "/add")
    public R add(@RequestBody Comment comment){
        boolean save = commentService.save(comment);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "更新")
    @PostMapping(path = "/updated")
    public R update(@RequestBody Comment comment){
        boolean flush = commentService.updateById(comment);
        if(flush){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

